<template>
  <el-dialog
    title="预览文章"
    :visible="showPreviewDialog"
    width="800px"
    @close="handleClose"
  >
    <div class="title">
      <h2>{{ currentArticle.title }}</h2>
      <div>
        <span>{{ formatTime(currentArticle.createTime) }}</span>
        <span>{{ currentArticle.username }}</span>
        <span class="el-icon-view"></span>
        <span>{{ currentArticle.visits }}</span>
      </div>
      <div class="content" v-html="currentArticle.articleBody"></div>
    </div>
  </el-dialog>
</template>

<script>
import dayjs from 'dayjs'
export default {
  props: {
    // 控制对话框显示与隐藏
    showPreviewDialog: {
      type: Boolean,
      default: false
    },
    // 当前操作的文章
    currentArticle: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    handleClose () {
      this.$emit('update:showPreviewDialog', false)
    },
    // 格式化时间
    formatTime (time, str = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(time).format(str)
    }
  }
}
</script>

<style scoped lang='scss'>
.title {
  padding-bottom: 10px;
  border-bottom: 1px dashed #ccc;
}
.content {
  padding: 10px;
  background: #f5f5f5;
}
span {
  margin: 10px 0;
  padding: 0 5px;
}
</style>
